package main

import (
	"errors"
	"flag"
	"gitee.com/acicn/library"
	"log"
	"os"
)

var (
	optDockerConfig string
	optFrom         string
	optTag          string
)

func main() {
	var err error
	defer func(err *error) {
		if *err != nil {
			log.Println("exited with error:", (*err).Error())
			os.Exit(1)
		}
	}(&err)

	flag.StringVar(&optDockerConfig, "docker-config", "", "custom docker '--config' option")
	flag.StringVar(&optFrom, "from", "", "source repository")
	flag.StringVar(&optTag, "tag", "latest", "image tag")
	flag.Parse()

	found := false

	for _, task := range library.Mirrors {
		if task.From == optFrom {
			found = true
			if err = task.Do(library.MirrorTaskOptions{DockerConfig: optDockerConfig, Tag: optTag}); err != nil {
				return
			}
		}
	}

	if !found {
		err = errors.New("mirror configuration not found")
	}
}
